<?php
/**
 * Created by PhpStorm.
 * User: james
 * Date: 15-3-12
 * Time: 16:28
 */

class CreateCloudFeedsNodeProcessor extends FeedsNodeProcessor {

  /**
   * Creates a new node in memory and returns it.
   */
  protected function newEntity(FeedsSource $source) {
    $node = parent::newEntity($source);
    $source_config = $source->getConfigFor($this);
    if(!empty($source_config['bundle'])) {
      $node->type = $source_config['bundle'];
    }
    return $node;
  }

  public function sourceDefaults() {
    return array();
  }
  /**
   * Callback methods, exposes source form.
   */
  public function sourceForm($source_config) {

    if (empty($source_config)) {
      $source_config = $this->getConfig();
    }

    $info = $this->entityInfo();
    $form = array();

    if (!empty($info['entity keys']['bundle'])) {
      $form['bundle'] = array(
        '#type' => 'select',
        '#options' => $this->bundleOptions(),
        '#title' => !empty($info['bundle name']) ? $info['bundle name'] : t('Bundle'),
        '#default_value' => $source_config['bundle'],
      );
    }
    else {
      $form['bundle'] = array(
        '#type' => 'value',
        '#value' => $this->entityType(),
      );
    }

    return $form;

  }

  /**
   * Validation handler for sourceForm.
   */
//  public function sourceFormValidate(&$source_config) {}

  /**
   * A source is being saved.
   */
//  public function sourceSave(FeedsSource $source) {}
} 